package com.glamour.hlcity.dal.typeHandler;

public class EnumUtils {
	/**
	 * 根据枚举类型和代码查找枚举值
	 * 
	 * @param enumClass 枚举类型
	 * @param code 代码，大小写不敏感
	 * @return 枚举值
	 */
	public static <E extends HasCode> E findByCode(Class<E> enumClass, String code) {
		for(E enumeration : enumClass.getEnumConstants()){
			if(enumeration.code().equalsIgnoreCase(code)){
				return enumeration;
			}
		}
		StringBuffer sb = new StringBuffer();
		sb.append("non-existing code [" );
		sb.append(code);
		sb.append("] in enum type [");
		sb.append(enumClass.getName());
		sb.append("]");
		throw new IllegalArgumentException(sb.toString());
	}
	
}
